import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit, QLabel, QMessageBox
import os

class LinuxToolbox(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setWindowTitle("系统工具箱")
        layout = QVBoxLayout()

        # 左侧布局
        left_layout = QVBoxLayout()

        # 修复dpkg占用按钮
        fix_dpkg_button = QPushButton('修复dpkg占用')
        fix_dpkg_button.clicked.connect(self.fix_dpkg)
        left_layout.addWidget(fix_dpkg_button)

        # 修复依赖关系不满足按钮
        fix_deps_button = QPushButton('修复依赖关系不满足')
        fix_deps_button.clicked.connect(self.fix_deps)
        left_layout.addWidget(fix_deps_button)

        # 启用ssh按钮
        enable_ssh_button = QPushButton('启用ssh')
        enable_ssh_button.clicked.connect(self.enable_ssh)
        left_layout.addWidget(enable_ssh_button)

        # 关闭ssh按钮
        disable_ssh_button = QPushButton('关闭ssh')
        disable_ssh_button.clicked.connect(self.disable_ssh)
        left_layout.addWidget(disable_ssh_button)

        # 修改dns按钮
        dns_layout = QHBoxLayout()
        dns_label = QLabel('输入dns:')
        self.dns_input = QLineEdit()
        dns_layout.addWidget(dns_label)
        dns_layout.addWidget(self.dns_input)
        change_dns_button = QPushButton('修改dns')
        change_dns_button.clicked.connect(self.change_dns)
        dns_layout.addWidget(change_dns_button)
        left_layout.addLayout(dns_layout)

        # wine
        wine_install_button = QPushButton('安装wine')
        wine_install_button.clicked.connect(self.wine_install)
        left_layout.addWidget(wine_install_button)
        wine_layout = QHBoxLayout()
        wine_exe_label = QLabel('选择exe:')
        self.wine_exe_input = QLineEdit()
        wine_layout.addWidget(wine_exe_label)
        wine_layout.addWidget(self.wine_exe_input)
        select_exe_button = QPushButton('选择exe')
        select_exe_button.clicked.connect(self.select_exe)
        wine_layout.addWidget(select_exe_button)
        run_wine_button = QPushButton('运行exe')
        run_wine_button.clicked.connect(self.run_wine)
        wine_layout.addWidget(run_wine_button)
        left_layout.addLayout(wine_layout)

        # 安装apt软件按钮
        apt_layout = QHBoxLayout()
        apt_package_label = QLabel('输入软件包名称：')
        self.apt_package_input = QLineEdit()
        apt_layout.addWidget(apt_package_label)
        apt_layout.addWidget(self.apt_package_input)
        install_apt_button = QPushButton('安装软件')
        install_apt_button.clicked.connect(self.install_apt)
        apt_layout.addWidget(install_apt_button)
        left_layout.addLayout(apt_layout)

        layout.addLayout(left_layout)

        # 右侧布局
        right_layout = QVBoxLayout()

        # 系统信息显示
        cpu_model = os.popen('python3 /opt/com.zhsoft.tool/osfetch.py | grep CPU').read().strip()
        cpu_label = QLabel(f"{cpu_model}")
        right_layout.addWidget(cpu_label)

        # 读取机器主板型号
        board_model = os.popen('cat /sys/class/dmi/id/product_name').read().strip()
        boardright_label = QLabel(f"机器型号: {board_model}")
        right_layout.addWidget(boardright_label)

        # 读取内存信息
        mem_info = os.popen('python3 /opt/com.zhsoft.tool/osfetch.py | grep 内存').read().strip()
        mem_label = QLabel(f"{mem_info}")
        right_layout.addWidget(mem_label)

        # 读取GPU信息
        gpu_info = os.popen('lspci | grep -i "VGA\|3D\|2D"').read().strip()
        gpu_label = QLabel(f"GPU: {gpu_info}")
        right_layout.addWidget(gpu_label)

        # 读取内核版本
        kernel_version = os.uname().release
        kernel_label = QLabel(f"内核版本: {kernel_version}")
        right_layout.addWidget(kernel_label)

        # 读取机器名
        machine_name = os.uname().nodename
        machine_label = QLabel(f"机器名: {machine_name}")
        right_layout.addWidget(machine_label)

        # 读取开机时间
        uptime = os.popen('uptime -p').read().strip()
        uptime_label = QLabel(f"开机时间: {uptime}")
        right_layout.addWidget(uptime_label)

        # 读取分辨率
        resolution = os.popen('python3 /opt/com.zhsoft.tool/osfetch.py | grep 分辨率').read().strip()
        resolution_label = QLabel(f"{resolution}")
        right_layout.addWidget(resolution_label)

        # 读取用户主文件夹地址
        home_dir = os.path.expanduser("~")
        home_dir_label = QLabel(f"用户主文件夹地址: {home_dir}\n© ZhongHongSoftware 开发")
        right_layout.addWidget(home_dir_label)

        # 底部按钮布局
        bottom_layout = QHBoxLayout()
        logout_button = QPushButton('注销')
        logout_button.clicked.connect(self.logout)
        restart_button = QPushButton('重启')
        restart_button.clicked.connect(self.restart)
        shutdown_button = QPushButton('关机')
        shutdown_button.clicked.connect(self.shutdown)
        bottom_layout.addWidget(logout_button)
        bottom_layout.addWidget(restart_button)
        bottom_layout.addWidget(shutdown_button)
        right_layout.addLayout(bottom_layout)

        layout.addLayout(right_layout)

        self.setLayout(layout)

    #def update_system_info(self):
        #cmd = "uname -a; uname -m; uname -r; free -h; cat /proc/cpuinfo | grep 'model name' | uniq; lshw -C display"
        #output = subprocess.check_output(cmd, shell=True).decode('utf-8')
        #self.system_info.setText(output)

    def fix_dpkg(self):
        os.system("pkexec dpkg --configure -a")
        QMessageBox.information(self, '提示', '完成')

    def fix_deps(self):
        os.system("pkexec apt-get install -f")
        QMessageBox.information(self, '提示', '完成')

    def enable_ssh(self):
        os.system("pkexec systemctl enable ssh")
        QMessageBox.information(self, '提示', '完成')

    def disable_ssh(self):
        os.system("pkexec systemctl disable ssh")
        QMessageBox.information(self, '提示', '完成')

    def change_dns(self):
        dns = self.dns_input.text()
        os.system(f"echo {dns} | pkexec tee /etc/resolv.conf")
        QMessageBox.information(self, '提示', '完成')

    def select_exe(self):
        exe = self.wine_exe_input.text()
        self.wine_exe = exe

    def wine_install(self):
        os.system('pkexec apt install wine')
        QMessageBox.information(self, '提示', '安装完成')

    def run_wine(self):
        if not hasattr(self, 'wine_exe'):
            QMessageBox.warning(self, '警告', '请先选择exe文件')
            return
        os.system(f"WINEPREFIX=~/.wine wine {self.wine_exe}")

    def install_apt(self):
        package = self.apt_package_input.text()
        os.system(f"pkexec apt-get install {package}")
        QMessageBox.information(self, '提示', '安装完成')

    def logout(self):
        os.system("logout")

    def restart(self):
        os.system("reboot")

    def shutdown(self):
        os.system("poweroff")

if __name__ == '__main__':#
    app = QApplication(sys.argv)
    toolbox = LinuxToolbox()
    toolbox.show()
    sys.exit(app.exec_())
